package com.xiaoma.myaudience.biz.activity;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.adapter.PersonalMypartActivityAdapter;
import com.xiaoma.myaudience.biz.task.DataAsyncTask;
import com.xiaoma.myaudience.biz.task.MyPartAllTask;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.biz.view.LoadingListView;
import com.xiaoma.myaudience.util.PrefHelper;
import com.xiaoma.myaudience.util.activity.BaseActivity;

/**
 * @author android
 * @descripton 我参与的活动
 */
public class MyPartActivity extends BaseActivity implements OnClickListener {
	
	private static final String TAG = "ActivitySelectActivity";
	private Map<String, Object> mObjMap;
	private List<Map<String, Object>> mList;


	private MyPartAllTask mPartAllTask;
	private PersonalMypartActivityAdapter mMyPartActivityAdapter;
	private List<Map<String, Object>> mAllList;
    private String mEventId;
    private LoadingListView mLoadingListView;
    private int mIndex = -1;
    private String dateline="0";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_partactivity_layout);
		
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			mIndex = bundle.getInt("index");
		}
		
		mLoadingListView = (LoadingListView) findViewById(R.id.hot_elv);

		setNetEaseTitleBarVisibility(View.VISIBLE);
		setNetEaseTitle("我的活动");
		setNetEaseTitleGravity(Gravity.CENTER);
		setNetEaseTitleBarExtra(R.layout.video_detail_extra_layout);

		findViewById(R.id.btn_back).setOnClickListener(this);
		execute(null);
		mLoadingListView.hideLoadingMoreLayout();
	}


	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.btn_back){
			MyPartActivity.this.finish();
		}
		//点击获取更多
		if(v.getId() == R.id.list_more_btn){
			execute(null);
		}
	}
	
	private void cancel() {
		if (mPartAllTask != null) {
			mPartAllTask.cancel(true, true);
		}
		mPartAllTask = null;
	}
	
	
	@Override
	public void onPostExecute(int what, Object o) {
		super.onPostExecute(what, o);
		List<Map<String, Object>> objMap = (List<Map<String, Object>>) o;
		switch (what) {
		case DataAsyncTask.TASK_ACTIVITY_MYPARTACTIVITY:
			if (objMap == null) {
				return;
			}
			
			initData(objMap);
			break;
		default:
			break;
		}
	}
	
	
	private void initData(List<Map<String, Object>> list) {
		mList = list;
		mLoadingListView.hideLoadingMoreLayout();
		
		if(list.size() == 0 || list==null){
			if(dateline.equals("0")){
				Tips("还未参加任何活动");
				MyPartActivity.this.finish();
				return;
			}else{
				mLoadingListView.hideLoadingMoreLayout();
			return;
			}
		}
		
		//初始化dateline
		dateline = (String)list.get(0).get("dateline");
		if(mMyPartActivityAdapter==null){
			mMyPartActivityAdapter = new PersonalMypartActivityAdapter(this, list);
		}
		mLoadingListView.getListView().setAdapter(mMyPartActivityAdapter);
		if(mIndex!=-1){
			mLoadingListView.getListView().setSelection(mIndex);
		}else{
			mLoadingListView.getListView().setSelection(0);
		}
		mLoadingListView.getLoadingMoreButton().setOnClickListener(this);
		mMyPartActivityAdapter.notifyDataSetChanged();
//    	mLoadingListView.finishClickDefaultMoreLayout();
	}
	
	private void execute(Map<String, String> params) {
		cancel();
		mPartAllTask = new MyPartAllTask(this,
				DataAsyncTask.TASK_ACTIVITY_MYPARTACTIVITY, this);
		Map<String, String> cookie = new HashMap<String, String>();
		cookie.put(Constant.LOGIN_COOKIE1, PrefHelper.getString(
				MyPartActivity.this, Constant.LOGIN_COOKIE1, ""));
		cookie.put(Constant.LOGIN_COOKIE2, PrefHelper.getString(
				MyPartActivity.this, Constant.LOGIN_COOKIE2, ""));
		cookie.put("dateline",dateline);
		//cookie.putAll(params);
		mPartAllTask.execute(cookie);
	}
	
    @Override
    public boolean onCreateOptionsNeteaseMenu(Menu menu) {
        return true;
    }
}
